package proxy;

import java.io.IOException;

import org.apache.http.io.SessionInputBuffer;
import org.apache.http.io.SessionOutputBuffer;

public class TransferToClient extends Thread{
	private SessionInputBuffer in = null;
	private SessionOutputBuffer out = null;
	private HttpSession session;
	
	public TransferToClient(HttpSession session, SessionInputBuffer in, SessionOutputBuffer out){
		this.in = in;
		this.out = out;
		this.session = session;
	}
	
	public void run(){
		int reading = -1;
		byte[] buffered = new byte[8192];
		while (true){
			try {
				reading = in.read(buffered, 0, buffered.length);
				if (reading == -1){
					break;
				}
				out.write(buffered, 0, reading);
				out.flush();
			} catch (IOException e) {
				//e.printStackTrace();
				break;
			}
		}
		try {
			out.flush();
			session.close();
			session.removeLastConnection();
		} catch (IOException e) {
		}
	}
}
